{{ template "_internal/google_analytics.html" . }}
<div class="mask"></div>
<script src="{{ .Site.BaseURL }}js/jquery.min.js"></script>
<script src="{{ .Site.BaseURL }}js/bootstrap.min.js"></script>
<script src="{{ .Site.BaseURL }}js/jstree.min.js"></script>

<script src="{{ .Site.BaseURL }}js/jquery.cookie.min.js"></script>
<script src="{{ .Site.BaseURL }}js/jquery.waypoints.min.js"></script>
<script src="{{ .Site.BaseURL }}js/jquery.counterup.min.js"></script>
<script src="{{ .Site.BaseURL }}js/jquery-parallax.js"></script>
<!-- <script src="https://vjs.zencdn.net/7.8.2/video.js"></script> -->


<!-- used to for mobile -->
<script src="{{ .Site.BaseURL }}js/flexible.js"></script>
<script src="{{ .Site.BaseURL }}js/common.js?t={{ .Site.Params.timestamp }}"></script>
<script src="{{ .Site.BaseURL }}js/dist/jso.js"></script>

<!-- custom -->
<script src="{{ .Site.BaseURL }}js/swiper.min.js"></script>
<script src="{{ .Site.BaseURL }}js/openGauss.js?t={{ .Site.Params.timestamp }}"></script>
<script src="{{ .Site.BaseURL }}js/cla.js?t={{ .Site.Params.timestamp }}"></script>
<div id="mask" onclick="hideNav()"></div>

<script type="text/javascript">
    $(document).ready(function () {
        function getRelativePath() {
            var url = location.href;
            var arrURL = url.split("//");
            return arrURL[1].substring(arrURL[1].indexOf("/"));
        }

        function getCurrentID(wholeData, relativeURL) {
            for (var j in wholeData) {
                var href = wholeData[j].a_attr.href;
                var text = wholeData[j].text;
                //debugger;
                if ((href == relativeURL) || (text.indexOf(relativeURL) >= 0)) {
                    console.log(wholeData[j].id);
                    return wholeData[j].id;
                } else {
                    if (wholeData[j].children.length > 0) {
                        var v = getCurrentID(wholeData[j].children, relativeURL);
                        if (v != "") {
                            return v;
                        }
                    }
                }
            }
            return "";
        }

        if ($('#docstreeview').length) {
            $('#docstreeview').jstree();
            $('#docstreeview').jstree().hide_dots();
            $('#docstreeview').jstree().hide_icons();
            $('#docstreeview').on("changed.jstree", function (e, data) {
               
                const tierData = data.node.parents
                tierData.unshift(data.node.id)
                tierData.pop()
               
                getTiername(tierData)

                if (data.node) {
                    var link = data.node.a_attr.href;
                    if (link == "" || link == "#") {
                        var aElementID = "#" + data.node.id + "_anchor";
                        var aElement = $(aElementID).find("a");
                        if (aElement.length) {
                            link = aElement.attr("href");
                        }
                    }
                   
                    var relativeURL = getRelativePath();
                    
                    if (link != relativeURL) {
                        location.href = link;
                    }
                }
            });

            /* select the node by the relative url
            var currentAElement = $('#docstreeview').find("a[href='" + getRelativePath() + "']");
            if (currentAElement.length) {
                console.log("click");
                currentAElement.trigger("click");
            }*/

            $("#docstreeview").on("ready.jstree", function (e, data) {
                $("#docstreeview").fadeIn("slow")
                var relativeURL = getRelativePath();
                var wholeData = $("#docstreeview").jstree().get_json();
                var currentID = getCurrentID(wholeData, relativeURL);
                
                $("#docstreeview").jstree()._open_to(currentID);
                $('#docstreeview').jstree().select_node(currentID);
                $('#docstreeview').jstree().open_node(currentID);

                // 获取上/下一页 URL                 
                let result = [];
                let nodeList = [];
                /*把树状平铺，用于查看是否有上一页下一页*/
                function getAllData(Arr) {
                    for (var i = 0; i < Arr.length; i++) {
                        var tempNode = {};
                        tempNode.id = Arr[i].id;
                        var href = Arr[i].a_attr.href;
                        if (href === '#') {
                            //转为jq对象特殊处理
                            var jqNode = $(Arr[i].text);
                            tempNode.href = jqNode.find('a').attr('href');
                        } else {
                            tempNode.href = href;
                        }
                        nodeList.push(tempNode);
                        if (Arr[i].children !== []) {
                            getAllData(Arr[i].children);
                        }
                    }
                };
                /*获取上一页、下一页*/
                function nextAndPevious(arr, id) {
                    for (var i = 0; i < arr.length; i++) {
                        if (arr[i].id === id) {
                            if (i === 0) {
                                result.push(null);
                            } else {
                                result.push(arr[i - 1].href);
                            }
                            if (i === arr.length - 1) {
                                result.push(null);
                            } else {
                                result.push(arr[i + 1].href);
                            }
                        }
                    }
                    return result;
                };
                getAllData(wholeData);
                nextAndPevious(nodeList, currentID, result);
               
                // 将 url 赋给按钮
                result[0] == null ? $('.pager .previous').addClass('disabled') : $('.pager .previous a').attr('href', result[0]);
                result[1] == null ? $('.pager .next').addClass('disabled') : $('.pager .next a').attr('href', result[1]);
            });
        }
        function showNav() {
            $('#menu').css({
                "transform": "translateX(0)"
            }).attr("onoff", "on");
            $('.mask').addClass('maskLayer');
        }
        function hideNav() {
            $('#menu').css({
                "transform": "translateX(-100%)"
            }).attr("onoff", "off");
            $('.mask').removeClass('maskLayer');
        }

        $("#slideNav").bind('click', function () {
            if ($('#menu').attr("onoff") == "on") {
                hideNav();
            } else {
                showNav();
            }
        })

        // 点击遮罩层隐藏
        $('.mask').bind('click', function() {
            if ($('#menu').attr("onoff") == "on") {
                hideNav();
            } else {
                showNav();
            }
        })

        /* 获取当前所在层级名称以及父层级名称 */
        function getTiername(data) {
            const iterName = [],iterUrl = []
            if(Array.isArray(data)) {
                data.map((item) => {
                    iterName.push($('#'+item+'_anchor').text()) 
                    iterUrl.push($('#'+item+'_anchor').attr('href'))
                })
            }
            
            let aNode = [];
            for(let i=0;i<iterName.length;i++) {
                aNode.push(i == (iterName.length - 1) ? `<a href="${iterUrl[i]}">${iterName[i]}</a>` : `<a href="${iterUrl[i]}">${iterName[i]}</a><span>&gt</span>`)
            }
            $('.breadcrumbs').append(aNode.toString())
        }

        /* 解决滚动后导航栏fixed脱离文档流导致滚动条不能到底 */
        if (/(iPhone|iPad|iPod|iOS|Android)/i.test(navigator.userAgent)) {
            console.log("这是手机")
        }else {
            $(window).scroll(function(){
                if($(document).scrollTop()>=$('.navbar-affixed-top').height()) {
                    $('#heading-breadcrumbs').addClass('breadcrumbs_top')
                }else {
                    $('#heading-breadcrumbs').removeClass('breadcrumbs_top')
                }
            });
        }
        
        /* 给每个pre添加复制代码功能 */
        let preList = $(".markdown pre");
        for(let pre of preList) {
            let btn = $("<span class='btn_pre_copy'>{{ i18n "copy_code" }}</span>");
            btn.prependTo(pre);
        }

        $('.btn_pre_copy').click(function() {
            let copyNode = $("<textarea></textarea>");
            copyNode.text($(this).next()[0].innerText);
            $('body').append(copyNode);
            copyNode.select();
            document.execCommand("Copy");
            copyNode.remove();
            $(this)[0].innerHTML = "{{ i18n "copy_code_success" }}"
            setTimeout(()=>{
                $(this)[0].innerHTML = "{{ i18n "copy_code" }}"
            },1500)
        })

        
    });
</script>
